package com.glsc.ngateway.opmanage.controller.xinyi;


import com.glsc.ngateway.common.api.platform.resp.PlatformResponse;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.User;
import com.glsc.ngateway.common.base.domain.mysql.opmanagemain.deposit.DepositoryBank;
import com.glsc.ngateway.common.base.domain.mysql.opmanagemain.xinyi.XinYiDefineTemplate;
import com.glsc.ngateway.common.base.domain.mysql.opmanagemain.xinyi.XinYiDefineTemplateItem;
import com.glsc.ngateway.opmanage.aspect.UserCache;
import com.glsc.ngateway.opmanage.dto.XinYiDefineDto;
import com.glsc.ngateway.opmanage.service.xinyi.XinYiDefineTemplateItemService;
import com.glsc.ngateway.opmanage.service.xinyi.XinYiDefineTemplateService;
import com.google.common.collect.Lists;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 新意指令自定义指令执行记录
 */
@RestController
@RequestMapping("/api/xinYiDefineTemplate")
@Slf4j
public class XinYiDefineTemplateController {

    @Resource
    private UserCache userCache;

    @Resource
    private XinYiDefineTemplateService xinYiDefineTemplateService;

    @Resource
    private XinYiDefineTemplateItemService itemService;

    /**
     * 分页查找
     *
     * @param dto
     * @param request
     * @return
     */
    @PostMapping("/page")
    public PlatformResponse<Page<XinYiDefineTemplate>> page(@RequestBody XinYiDefineDto dto, HttpServletRequest request) {

        Map<String, Object> param = new HashMap<>();
        if (StringUtils.isNotBlank(dto.getName())) {
            param.put("LIKE_name", dto.getName());
        }
        if (StringUtils.isNotBlank(dto.getType())) {
            param.put("LIKE_type", dto.getType());
        }
        Page<XinYiDefineTemplate> page = xinYiDefineTemplateService.page(dto.getPageNo(), dto.getPageSize(), param);

        return PlatformResponse.successData(page);
    }

    /**
     * 根据id查找
     *
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public PlatformResponse<XinYiDefineTemplate> findTemplateById(@ApiParam(required = false, value = "id") @RequestParam(required = true) Integer id) {
        XinYiDefineTemplate template = xinYiDefineTemplateService.findById(id);
        template.setItems(Lists.newArrayList());
        List<XinYiDefineTemplateItem> items = itemService.findByTemplateId(id);
        if (CollectionUtils.isNotEmpty(items)) {
            template.setItems(items);
        }
        return PlatformResponse.successData(template);
    }

    /**
     * 保存接口
     *
     * @param obj
     * @param request
     * @return
     */
    @PostMapping("/save")
    public PlatformResponse<XinYiDefineTemplate> save(@RequestBody XinYiDefineTemplate obj, HttpServletRequest request) {
        User user = userCache.get(request);
        obj.setCreateId(user.getId());
        obj.setCreateName(user.getName());
        return PlatformResponse.successData(xinYiDefineTemplateService.save(obj));
    }

    /**
     * 根据id删除接口
     *
     * @param obj
     * @param request
     * @return
     */
    @PostMapping("/delete")
    public PlatformResponse<?> delete(@RequestBody XinYiDefineTemplate obj, HttpServletRequest request) {
        xinYiDefineTemplateService.deleteById(obj.getId());
        return PlatformResponse.success();
    }

    /**
     * 发送新意指令
     *
     * @param id
     * @param request
     * @return
     */
    @GetMapping("/send")
    public PlatformResponse<?> send(@ApiParam(required = false, value = "id") @RequestParam(required = true) Integer id, HttpServletRequest request) {
        User user = userCache.get(request);
        xinYiDefineTemplateService.send(id, user);
        return PlatformResponse.success();
    }


}
